<?php

function get_nested_categories($categories, $parent_id, $step, $openones){	
	$r='';
	foreach($categories as $cat){
			if($cat->parent_id==$parent_id){
				$r.='<div class="category_item">';	
				$r.='<div class="category_head">';		
			
				$params=array('EDIT'=>'input', 'EMPTY'=>'media', 'DEL'=>'');
				$r.=sigi::actionbox_params('category', $cat->id,$params);
				
				
				$params=array(array('title'=>'add Media', 'link'=>CONTDIR.'media/form/input?parent_id='.$cat->id));
				$params[]=array('title'=>'add Multiple Mediafiles', 'link'=>CONTDIR.'media/form/inputmultiple?parent_id='.$cat->id);
				$params[]=array('title'=>'add Multiple Mediafiles Applet (BETA!)', 'link'=>CONTDIR.'media/form/inputmultiple_applet?parent_id='.$cat->id);
				
				$params[]=array('title'=>'add Category', 'link'=>CONTDIR.'category/form/input?parent_id='.$cat->id);
				$params[]=array('title'=>'add Multiple Categories', 'link'=>CONTDIR.'category/form/multiple?parent_id='.$cat->id);
				
				$r.=sigi::addbox($params);
				
				$isopen=in_array($cat->id, $openones);
				if($isopen){
					$class='selected';
					$linkid=$cat->parent_id;
				}else{
					$class='';
					$linkid=$cat->id;
				}
				
				$r.= html::anchor(CONTDIR.'media/listall/'.$linkid, $cat->title, array('class'=>$class));
				$r.='</div>';
				
				if($isopen){
					foreach($cat->media as $media){
						
						$medview=new View('media/mediaelm_in_list');
						$medview->media=$media;
						$r.=$medview;
						
					}
					
					$r.=get_nested_categories($categories, $cat->id, $step+1,$openones);
				}
				$r.='</div>';
			}			
		}
	return $r;
}
?>

<div class='contentheader'>
<?
$params[]=array('title'=>'add Category', 'link'=>CONTDIR.'category/form/input?parent_id=0');
$params[]=array('title'=>'add Multiple Categories', 'link'=>CONTDIR.'category/form/multiple?parent_id=0');

echo sigi::addbox($params);
?>

<h1>alle Medienelemente</h1>
</div>

<?php
/*
// Unkategorisierte Medien
echo'<div class="category_item">';
echo '<div class="category_head">';

$isopen=($id=='nocat');

if($isopen){
	$class='selected';
	$linkid='';
}else{
	$class='';
	$linkid='nocat';
}


echo html::anchor(CONTDIR.'media/listall/'.$linkid, 'Unkategorisiert', array('class'=>$class));
echo '</div>';

$medias=ORM::factory('media')->where('category_id', 0)->find_all();

if($isopen){
	foreach($medias as $media){
		$medview=new View('media/mediaelm_in_list');
		$medview->media=$media;
		echo $medview;				
	}
}


echo '</div>';
*/


// Medien in Kategorien

$selected=ORM::factory('category',$id);
$openones=$selected->get_openones();
$categories=ORM::factory('category');

echo get_nested_categories($categories->find_all()->as_array(),  0, 0, $openones);

?>